<?php
/**
 * EventsController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-20 19:20:33
 * @modified    2023-02-20 19:20:33
 *
 */

namespace App\Http\Controllers;

use App\Http\Resources\EventResource;
use App\Repositories\EventRepo;
use App\Repositories\WorkOrderRepo;
use Illuminate\Http\Request;

class EventsController extends Backend
{

    /**
     * @OA\Get(
     *     path="/events/params",
     *     tags={"Event"},
     *     summary="获取事件参数列表",
     *     description="获取事件参数列表",
     *     operationId="event_params",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function params()
    {
        return message('获取成功', true, EventRepo::params());
    }


    /**
     * @OA\Get(
     *     path="/events",
     *     tags={"Event"},
     *     summary="获取事件列表",
     *     description="获取事件列表",
     *     operationId="event_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="status", in="query", description="状态(待分配、已分配、处理中、已完成、已关闭)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="about_me", in="query", description="创建视角: assist、master、create（我协助、我负责、我创建）", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="type", in="query", description="类型(服务请求、客户报修、服务订单、维保服务、项目管理、意见投诉)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="date", in="query", description="日期(今天 today, 本周内 week, 本月 month, 本年 year)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="customer_name", in="query", description="客户名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="contact_name", in="query", description="客户联系人", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="contact_phone", in="query", description="联系电话", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="number", in="query", description="工单编号(模糊)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="export", in="query", description="是否导出(需要导出时传1)", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="ids", in="query", description="导出ID(1,2,3,...,n)不传或为空时，全部导出", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index()
    {
        try {
            $events = EventRepo::getList();
            $export = intval(\request('export', ''));
            if ($export) {
                return message('获取成功', true, $events);
            }
            return list_message('获取成功', true, $events);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/events/{id}",
     *     tags={"Event"},
     *     summary="获取事件详情",
     *     description="获取事件详情",
     *     operationId="event_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="事件ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        return message('获取成功', true, EventRepo::fetch($id));
    }

    /**
     * @OA\Get(
     *     path="/events/{id}/work_order",
     *     tags={"Event"},
     *     summary="获取工单详情",
     *     description="获取工单详情",
     *     operationId="event_work_order",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="事件ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function work_order(Request $request, $id)
    {
        try {
            $wo = WorkOrderRepo::fetchByEventId($id);
            return message('获取成功', true, $wo);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/events",
     *     tags={"Event"},
     *     summary="新建事件",
     *     description="新建事件",
     *     operationId="event_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"type", "customer_id", "customer_user_id", "contact_telephone", "province_id", "city_id", "county_id", "address", "product_id", "plan_at", "description", "user_id"},
     *              @OA\Property(property="type", type="string", example="事件类型(见参数列表)", description="事件类型"),
     *              @OA\Property(property="customer_id", type="integer", example="客户ID", description="客户ID"),
     *              @OA\Property(property="customer_user_id", type="integer", example="客户联系人ID", description="服务类型"),
     *              @OA\Property(property="contact_telephone", type="string", example="联系人电话", description="联系人电话"),
     *              @OA\Property(property="province_id", type="integer", example="省份ID", description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example="城市ID", description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="区县ID", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="详细地址", description="详细地址"),
     *              @OA\Property(property="product_id", type="integer", example="关联商品ID", description="关联商品ID"),
     *              @OA\Property(property="plan_at", type="string", example="计划时间", description="购买渠道"),
     *              @OA\Property(property="description", type="string", example="情况说明", description="情况说明"),
     *              @OA\Property(property="user_id", type="integer", example="负责人ID", description="负责人ID"),
     *              @OA\Property(property="images", type="array",@OA\Items())
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        $id = 0;
        $createdBy = (int)registry('user_id');
        $createdType = EventRepo::CREATED_USER;
        try {
            $event = EventRepo::store($id, $createdBy, $createdType);
            return message('创建成功', true, $event);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, null);
        }
    }

    /**
     * @OA\Post(
     *     path="/events/{id}/allot",
     *     tags={"Event"},
     *     summary="分配事件",
     *     description="分配事件",
     *     operationId="event_allot",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="事件ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"user_id", "assist_user_ids"},
     *              @OA\Property(property="user_id", type="integer", example=0, description="负责人ID"),
     *              @OA\Property(
     *                  property="assist_user_ids",
     *                  type="array",
     *                  @OA\Items(),
     *              )
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function allot(Request $request, $id)
    {
        try {
            $event = EventRepo::allot($id);
            return message('分配成功', true, $event);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, null);
        }
    }

    /**
     * @OA\Post(
     *     path="/events/{id}/correlation",
     *     tags={"Event"},
     *     summary="事件关联",
     *     description="事件关联",
     *     operationId="event_correlation",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="事件ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"customer_id"},
     *              @OA\Property(property="customer_id", type="integer", example="客户ID(必填)", description="客户ID"),
     *              @OA\Property(property="customer_user_id", type="integer", example="客户联系人ID(必填、客户所属联系人ID)", description="客户联系人ID"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function correlation(Request $request, $id)
    {
        try {
            EventRepo::correlation($id);
            return message('绑定成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function start(Request $request, $id)
    {
        return ;
        try {
            $event = EventRepo::start($id);
            return message('开始处理成功', true, $event);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, null);
        }
    }

    /**
     * @OA\Post(
     *     path="/events/{id}/receipt",
     *     tags={"Event"},
     *     summary="事件回执",
     *     description="事件回执",
     *     operationId="event_receipt",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="事件ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"description"},
     *              @OA\Property(property="description", type="string", example="处理结果", description="处理结果"),
     *              @OA\Property(property="images", type="array", @OA\Items())
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function receipt(Request $request, $id)
    {
        try {
            $event = EventRepo::receipt($id);
            return message('回执成功', true, $event);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, null);
        }
    }
}